<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>qLibc: extensions/qtokenbucket.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">qLibc
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('qtokenbucket_8c.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">qtokenbucket.c File Reference</div>  </div>
</div><!--header-->
<div class="contents">

<p>Token Bucket implementation.  
<a href="#details">More...</a></p>

<p><a href="qtokenbucket_8c_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a8da36d9c0f027f0a24086526593e2eab"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="qtokenbucket_8c.html#a8da36d9c0f027f0a24086526593e2eab">qtokenbucket_init</a> (qtokenbucket_t *bucket, int init_tokens, int max_tokens, int tokens_per_sec)</td></tr>
<tr class="memdesc:a8da36d9c0f027f0a24086526593e2eab"><td class="mdescLeft">&#160;</td><td class="mdescRight">Initialize the token bucket.  <a href="#a8da36d9c0f027f0a24086526593e2eab">More...</a><br/></td></tr>
<tr class="separator:a8da36d9c0f027f0a24086526593e2eab"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aec09c4e8a1ef411e4a5ee6d9036dbca0"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="qtokenbucket_8c.html#aec09c4e8a1ef411e4a5ee6d9036dbca0">qtokenbucket_consume</a> (qtokenbucket_t *bucket, int tokens)</td></tr>
<tr class="memdesc:aec09c4e8a1ef411e4a5ee6d9036dbca0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Consume tokens from the bucket.  <a href="#aec09c4e8a1ef411e4a5ee6d9036dbca0">More...</a><br/></td></tr>
<tr class="separator:aec09c4e8a1ef411e4a5ee6d9036dbca0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a5464647c7e3874f8517fdbe787ee6ce3"><td class="memItemLeft" align="right" valign="top">long&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="qtokenbucket_8c.html#a5464647c7e3874f8517fdbe787ee6ce3">qtokenbucket_waittime</a> (qtokenbucket_t *bucket, int tokens)</td></tr>
<tr class="memdesc:a5464647c7e3874f8517fdbe787ee6ce3"><td class="mdescLeft">&#160;</td><td class="mdescRight">Get the estimate time until given number of token is ready.  <a href="#a5464647c7e3874f8517fdbe787ee6ce3">More...</a><br/></td></tr>
<tr class="separator:a5464647c7e3874f8517fdbe787ee6ce3"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Token Bucket implementation. </p>
<p>Current implementation is not thread-safe.</p>
<p>More information about token-bucket: <a href="http://en.wikipedia.org/wiki/Token_bucket">http://en.wikipedia.org/wiki/Token_bucket</a></p>
<div class="fragment"><div class="line">qtokenbucket_t bucket;</div>
<div class="line"><a class="code" href="qtokenbucket_8c.html#a8da36d9c0f027f0a24086526593e2eab">qtokenbucket_init</a>(&amp;bucket, 500, 1000, 1000);</div>
<div class="line"><span class="keywordflow">while</span> (1) {</div>
<div class="line">  <span class="keywordflow">if</span> (<a class="code" href="qtokenbucket_8c.html#aec09c4e8a1ef411e4a5ee6d9036dbca0">qtokenbucket_consume</a>(&amp;bucket, 1) == <span class="keyword">false</span>) {</div>
<div class="line">    <span class="comment">// Bucket is empty. Let&#39;s wait</span></div>
<div class="line">    usleep(<a class="code" href="qtokenbucket_8c.html#a5464647c7e3874f8517fdbe787ee6ce3">qtokenbucket_waittime</a>(&amp;bucket, 1) * 1000);</div>
<div class="line">    <span class="keywordflow">continue</span>;</div>
<div class="line">  }</div>
<div class="line">  <span class="comment">// Got a token. Let&#39;s do something here.</span></div>
<div class="line">  do_something();</div>
<div class="line">}</div>
</div><!-- fragment --> 
<p>Definition in file <a class="el" href="qtokenbucket_8c_source.html">qtokenbucket.c</a>.</p>
</div><h2 class="groupheader">Function Documentation</h2>
<a class="anchor" id="a8da36d9c0f027f0a24086526593e2eab"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void qtokenbucket_init </td>
          <td>(</td>
          <td class="paramtype">qtokenbucket_t *&#160;</td>
          <td class="paramname"><em>bucket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>init_tokens</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>max_tokens</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>tokens_per_sec</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Initialize the token bucket. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">init_tokens</td><td>the initial number of tokens. </td></tr>
    <tr><td class="paramname">max_tokens</td><td>maximum number of tokens in the bucket. </td></tr>
    <tr><td class="paramname">tokens_per_sec</td><td>number of tokens to fill per a second. </td></tr>
  </table>
  </dd>
</dl>

<p>Definition at line <a class="el" href="qtokenbucket_8c_source.html#l00076">76</a> of file <a class="el" href="qtokenbucket_8c_source.html">qtokenbucket.c</a>.</p>

</div>
</div>
<a class="anchor" id="aec09c4e8a1ef411e4a5ee6d9036dbca0"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">bool qtokenbucket_consume </td>
          <td>(</td>
          <td class="paramtype">qtokenbucket_t *&#160;</td>
          <td class="paramname"><em>bucket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>tokens</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Consume tokens from the bucket. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">bucket</td><td>tockenbucket object. </td></tr>
    <tr><td class="paramname">tokens</td><td>number of tokens to request.</td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>return true if there are enough tokens, otherwise false. </dd></dl>

<p>Definition at line <a class="el" href="qtokenbucket_8c_source.html#l00093">93</a> of file <a class="el" href="qtokenbucket_8c_source.html">qtokenbucket.c</a>.</p>

</div>
</div>
<a class="anchor" id="a5464647c7e3874f8517fdbe787ee6ce3"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">long qtokenbucket_waittime </td>
          <td>(</td>
          <td class="paramtype">qtokenbucket_t *&#160;</td>
          <td class="paramname"><em>bucket</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>tokens</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p>Get the estimate time until given number of token is ready. </p>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">tokens</td><td>number of tokens</td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>estimated milliseconds </dd></dl>

<p>Definition at line <a class="el" href="qtokenbucket_8c_source.html#l00109">109</a> of file <a class="el" href="qtokenbucket_8c_source.html">qtokenbucket.c</a>.</p>

</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_40c73bf2095fd0473b1b85ccdcac2768.html">extensions</a></li><li class="navelem"><a class="el" href="qtokenbucket_8c.html">qtokenbucket.c</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.6 </li>
  </ul>
</div>
</body>
</html>
